package sbc.corso;

import java.util.List;
import java.util.Vector;

import org.xvsm.api.core.Capi;
import org.xvsm.api.core.ContainerRef;
import org.xvsm.api.core.CoordinationTypes;
import org.xvsm.api.core.Entry;
import org.xvsm.api.core.Tuple;
import org.xvsm.api.core.notification.NotificationID;
import org.xvsm.api.core.notification.NotificationListener;
import org.xvsm.client.ClientCapi;

import sbc.corso.shareable.Platine;
import sbc.corso.shareable.PlatinenFabrikInfo;
import sbc.corso.shareable.Sensor;
import sbc.ms.Consumer;
import sbc.ms.Util;
import corso.lang.CorsoConnection;
import corso.lang.CorsoException;
import corso.lang.CorsoWriteException;

public class Bridge extends Thread implements NotificationListener {
	
    private String url;
    private Capi capi;
    private ContainerRef cref;
    private List<Entry> elements;
	
	public Bridge (String url) {
		this.url = url;
		
		System.out.println ("Bridge @ " + url + " init");
		
		try	{	
			/*
			 * get Mozart Space Container
			 */
			this.capi = new ClientCapi(url);
			this.capi.init(false);
			this.cref = Util.getOrCreateNamedContainer(capi, "Container", CoordinationTypes.FIFO);
			
			/*
			 * take every tupel out of Mozart Space
			 */
			//Taker myTaker = new Taker(capi, cref);
			//myTaker.start();
			
			addListener(this);
			
			System.out.println("Bridge constructed");
		}
		catch (Exception e)	{
			System.out.println("e: " + e);
		}		
	}
	
	@Override
	public void run() {
		System.out.println("Bridge up and running");
	}
	
	public void sendNotification(NotificationID id, List<Entry> entries) {
		System.out.println("Bridge Notification fired: " + id + " - " + entries);
		
		/*
		 * get Platine/Sensor out ouf fired Entries
		 */
		Tuple t = (Tuple) entries.get(0);
		elements = t.getValue();	
		String corsoname = elements.get(0).getValue().toString();	
		String platinenid = elements.get(1).getValue().toString();
		
		System.out.println("corsoname: "  + corsoname);		
		System.out.println("platinenid: " + platinenid);
		
		try	{			 
			/*
			 * connect to Corso and
			 * get or create PlatinenFabrikInfo
			 */
			CorsoConnection con = CorsoUtil.getCorsoConnection();
			PlatinenFabrikInfo fabrik = CorsoUtil.getPlatinenFabrikInfo(con);
			
	        /*
	         * add either Platine or Sensor to Corso Space
	         */
	        if (elements.get(0).getValue().toString().equalsIgnoreCase("Platine"))	{
		        Platine myPlatine = new Platine(platinenid, CorsoUtil.NICHT_MONTIERT); 
		        System.out.println("add  platine: " + platinenid);
		        //System.out.println ("11111111: " + myPlatine.getFirstSensor().getId());
		        if (!platinenid.contains(" ("))	{
		        	fabrik.addPlatine(con, myPlatine);
		        	System.out.println("in corso schreiben");
		        }
	        }
	        else {
		        Sensor mySensor = new Sensor(platinenid, CorsoUtil.NICHT_MONTIERT); 
		        System.out.println("add sensor: " + platinenid);
				fabrik.addSensor(con, mySensor); 
	        }

	        System.out.println("writing data to corso space - ok"); 
	        con.disconnect();
		}
        catch (CorsoWriteException e2) {
            System.out.println("2Cannot create named oid " + e2);
            System.exit (-4);
        }
        catch (CorsoException e2) {
            System.out.println("3Cannot create named oid " + e2);
            System.exit (-4);
        }			
	}
	
	public void sendNotification(NotificationID id) {
		System.out.println("Bridge Notification fired: " + id);
	}
	
	public void addListener(NotificationListener listener) {
		System.out.println("addListener");
		Consumer c = new Consumer(url, listener);
		c.start();
	}
	
	public static void main(String args[])	{
		if (args.length < 1) {
			System.out.println("URL needed!");
			System.out.println("Usage: java -cp <classpath> sbc.corso.Bridge http://localhost:8080/xvsm/xvsm");
			System.exit(1);
		}
		Bridge mybridge = new Bridge(args[0]);
		mybridge.start();
	}    
}